package com.alks.function.service.impl;

import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PcCollectUser2;
import com.alks.entity.data.entity.PcCollectUserDept;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.pcCollectUser.PcCollectUser2DeptDto;
import com.alks.function.data.dto.pcCollectUser.PcCollectUser2Dto;
import com.alks.function.data.request.pcCollectUser.CollectUserRequest;
import com.alks.function.mapper.PcCollectUserMapper;
import com.alks.function.service.PcCollectUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class PcCollectUserServiceImpl implements PcCollectUserService {

    @Autowired
    private PcCollectUserMapper pcCollectUser2Mapper;


    /**
     * 查询用户
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCollectUser(CollectUserRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        List<PcCollectUser2Dto> list = pcCollectUser2Mapper.getCollectUser(companyId,request);

        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        return ResponseInfo.ok(map);
    }

    /**
     * 添加用户
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo addCollectUser(PcCollectUser2Dto request) {
        String companyId = UserIdThread.get().getCompanyId();
        //判断重复了没有
        CollectUserRequest collectUserRequest = new CollectUserRequest();
        collectUserRequest.setCollectUserName(request.getCollectUserName());
        List<PcCollectUser2Dto> list = pcCollectUser2Mapper.getCollectUser(companyId,collectUserRequest);
        if(list.size() > 0){
            return ResponseInfo.error("账户已存在");
        }
        //判空
        if(request.getCollectUserName() == null || request.getCollectUserPwd() == null){
            return ResponseInfo.error("账户或密码不能为空");
        }

        PcCollectUser2 user = new PcCollectUser2();
        BeanUtils.copyProperties(request,user);
        user.setSysDate(LocalDateTime.now());
        user.setSysName(UserIdThread.get().getUserName());

        int count = pcCollectUser2Mapper.addCollectUser(companyId,user);
        if(count > 0){
            return ResponseInfo.ok("添加成功");
        }else{
            return ResponseInfo.error("添加失败");
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo deleteCollectUser(String id) {
        int count = pcCollectUser2Mapper.deleteCollectUser(id);
        int count2 = pcCollectUser2Mapper.deleteCollectDeptBatch(id);
        if(count > 0){
            return ResponseInfo.ok("删除成功");
        }else{
            return ResponseInfo.error("删除失败");
        }
    }

    /**
     * 修改用户
     * @param request
     * @return
     */
    @Override
    public ResponseInfo updateCollectUser(PcCollectUser2Dto request) {
        String companyId = UserIdThread.get().getCompanyId();
        //如果修改登录名，要判断这个登录名字以前有没有
        CollectUserRequest collectUserRequest = new CollectUserRequest();
        collectUserRequest.setCollectUserName(request.getCollectUserName());
        List<PcCollectUser2Dto> list = pcCollectUser2Mapper.getCollectUser(companyId,collectUserRequest);
        String collectUserName = list.get(0).getCollectUserName();
        boolean equals = request.getCollectUserName().equals(collectUserName);
        if(list.size() > 0 && !equals){
            return ResponseInfo.error("账户重复");
        }

        PcCollectUser2 user = new PcCollectUser2();
        BeanUtils.copyProperties(request,user);

        int count = pcCollectUser2Mapper.updateCollectUser(companyId,user);
        if(count > 0){
            return ResponseInfo.ok("修改成功");
        }else{
            return ResponseInfo.error("修改失败");
        }
    }

    /**
     * 根据用户id查部门
     * @return
     */
    @Override
    public ResponseInfo getDeptByCollectUser(String id){
        List<PcCollectUser2DeptDto> list = pcCollectUser2Mapper.getDeptByCollectUser(id);
        HashMap map = new HashMap<>();
        PageInfo page = new PageInfo(list);
        map.put("total",page.getTotal());
        map.put("list",list);
        return ResponseInfo.ok(map);
    }

    /**
     * 删除采集部门
     * @param request
     * @return
     */
    @Override
    public ResponseInfo deleteCollectDept(PcCollectUser2DeptDto request) {
        String companyId = UserIdThread.get().getCompanyId();
        //判断部门是否存在：原来的部门不能为空
        if(request.getDeptNo() == null){
            throw new ServiceErrorException("请选中要删除的数据行");
        }
        int count = pcCollectUser2Mapper.deleteCollectDept(companyId,request);
        if(count > 0){
            return ResponseInfo.ok("删除成功");
        }else{
            return ResponseInfo.error("删除失败");
        }
    }

    /**
     * 修修改采集部门
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo updateCollectDept(PcCollectUser2DeptDto request) {
        String companyId = UserIdThread.get().getCompanyId();
        //如果修改的部门和原先部门不同，才判断这个部门是否存在
        //判断部门是否存在：原来的部门不能为空
        if(request.getDeptNo2() == null || request.getDeptNo() == null){
            throw new ServiceErrorException("部门编号不能为空");
        }
        //判断部门名称是否为空
        if(request.getDeptName2() == null){
            throw new ServiceErrorException("部门名称不能为空");
        }
        if(!request.getDeptNo().equals(request.getDeptNo2())){
            //查询 dept_no 是否已被其他用户使用
            boolean exists = pcCollectUser2Mapper.existsDeptNo(request.getDeptNo2(), request.getCollectUserId());
            if (exists) {
                throw new ServiceErrorException("部门编号已存在，不能重复添加");
            }
        }

        int count2 = pcCollectUser2Mapper.updateCollectDept(companyId,request);

        if(count2 > 0){
            return ResponseInfo.ok("修改成功");
        }else{
            return ResponseInfo.error("修改失败");
       }
    }

    /**
     * 添加采集部门
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo addCollectDept(PcCollectUser2DeptDto request) {
        //判空
        if(request.getDeptNo() == null || request.getDeptName() == null){
            return ResponseInfo.error("部门不能为空");
        }
        String companyId = UserIdThread.get().getCompanyId();

        // 1. 查询 dept_no 是否已被其他用户使用
        boolean exists = pcCollectUser2Mapper.existsDeptNo(request.getDeptNo(), request.getCollectUserId());
        if (exists) {
            throw new ServiceErrorException("部门编号已存在，不能重复添加");
        }

        PcCollectUserDept dept = new PcCollectUserDept();
        BeanUtils.copyProperties(request,dept);
        dept.setCompanyId(companyId);

        int count2 = pcCollectUser2Mapper.addCollectDept(companyId,dept);

        if(count2 > 0){
            return ResponseInfo.ok("添加成功");
        }else{
            return ResponseInfo.error("添加失败");
        }
    }

}
